local skel = fk.CreateSkill {
  name = "emo__fengwu",
}

Fk:loadTranslationTable{
  ["emo__fengwu"] = "凤舞",
  [":emo__fengwu"] = "当你造成或受到伤害时，你可以将体力值调整至2点，若你因此减少了体力值，则你本回合获得〖狂骨〗，否则你摸两张牌且本回合〖凤舞〗失效。伤害结算后，你可以使用一张【杀】。",

  ["#emo__fengwu-losehp"] = "凤舞：你可以将体力值扣减至2点，获得〖狂骨〗，结算后你可使用【杀】",
  ["#emo__fengwu-recover"] = "凤舞：你可以将体力值恢复至2点，摸两张牌，结算后你可使用【杀】",
  ["#emo__fengwu_delay"] = "凤舞",
  ["#emo__fengwu-use"] = "凤舞：你可以使用一张【杀】",
}

local can_trigger = function(self, event, target, player, data)
  return player == target and player:hasSkill(skel.name) and player.maxHp > 1
end

local on_cost = function (self, event, target, player, data)
  local prompt = player.hp > 2 and "#emo__fengwu-losehp" or "#emo__fengwu-recover"
  return player.room:askToSkillInvoke(player, { skill_name = skel.name, prompt = prompt })
end

local on_use = function (self, event, target, player, data)
  local room = player.room
  data.extra_data = data.extra_data or {}
  data.extra_data.emo__fengwuInvoker = data.extra_data.emo__fengwuInvoker or {}
  table.insertIfNeed(data.extra_data.emo__fengwuInvoker, player.id)
  local x = 2 - player.hp
  if x ~= 0 then
    -- 感觉有点危险的内部函数，reason得写nil不然报错
    room:changeHp(player, x, nil, skel.name)
    if player.dead then return end
  end
  if x < 0 then
    if not player:hasSkill("emo__kuanggu", true) then
      player.tag["emo__fengwu_kuanggu"] = true
      room:handleAddLoseSkills(player, "emo__kuanggu")
    end
  else
    room:invalidateSkill(player, skel.name, "-turn")
    player:drawCards(2, skel.name)
  end
end

skel:addEffect(fk.DamageCaused, {
  anim_type = "drawcard",
  can_trigger = can_trigger,
  on_cost = on_cost,
  on_use = on_use,
})

skel:addEffect(fk.DamageInflicted, {
  anim_type = "drawcard",
  can_trigger = can_trigger,
  on_cost = on_cost,
  on_use = on_use,
})

skel:addEffect(fk.DamageFinished, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return not player.dead and table.contains((data.extra_data or {}).emo__fengwuInvoker or {}, player.id)
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local use = room:askToUseCard(player, {
      skill_name = skel.name, pattern = "slash", prompt = "#emo__fengwu-use", extra_data = {bypass_times = true}
    })
    if use then
      use.extraUse = true
      room:useCard(use)
    end
  end,
})

skel:addEffect(fk.TurnEnd, {
  is_delay_effect = true,
  mute = true,
  can_trigger = function (self, event, target, player, data)
    return player.tag["emo__fengwu_kuanggu"]
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    player.tag["emo__fengwu_kuanggu"] = nil
    player.room:handleAddLoseSkills(player, "-emo__kuanggu")
  end,
})

return skel
